$import("fp.Preferences");
$import("fp.Storage");
$import("g.ReaderService");
$import("g.Account");
$import("g.Reader");
$import("fp.caches.FeedCache");
$import("fp.caches.ImageCache");
$import("fp.utils.ContentExtractor");



with ($ns("fp"))
{
    fp.AppBackgroundClass = function()
    {
        var me = this;
        
        me.init = function()
        {
            g.Account.bind("authenticated", _account_authenticated);
            
            if (fp.Preferences.get("userName") != null && fp.Preferences.get("password") != null)
            {
                if (navigator.onLine)
                {
                    console.log("Chrome is now online. Try to sign into Google.");
                    
                    g.Account.login({
                        email: fp.Preferences.get("userName"),
                        password: fp.Preferences.get("password")
                    });
                }
                else
                {
                    console.log("Chrome is now offline. Try to use local account.");
                    g.Account.localLogin();
                }
            }
            else
            {
                var firstTime = fp.Preferences.get("firstTime");
                if (!firstTime)
                {
                    fp.Preferences.set("firstTime", true);
                    window.open("Login.html");
                }
            }
        };
        
        
        
        function _account_authenticated(e)
        {
            fp.caches.FeedCache.updateCache();
        }
        
        return me;
    };
}